package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ct_start
 * @create 2021 -09 -15
 **/
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new org.springframework.web.cors.CorsConfiguration();

        // 配置跨域
            // 允许哪些头进行跨域
        corsConfiguration.addAllowedHeader("*");
            // 允许哪些方式进行跨域
        corsConfiguration.addAllowedMethod("*");

        corsConfiguration.addAllowedOrigin("*");

        corsConfiguration.setAllowCredentials(true);
        // 注册跨域路径的配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return  new CorsWebFilter(source);
    }
}
